/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.odk.collect.android.provider;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import org.odk.collect.android.application.Collect;

import android.app.Activity;
import android.database.Cursor;
import android.net.Uri;
import android.provider.BaseColumns;

/**
 * Convenience definitions for NotePadProvider
 */
public final class InstanceProviderAPI {
    public static final String AUTHORITY = "org.odk.collect.android.provider.odk.instances";

    // This class cannot be instantiated
    private InstanceProviderAPI() {}
    
    // status for instances
    public enum Status
    {
    	incomplete,
    	complete,
    	submitted,
    	submissionFailed;

    	private static final Map<String, Status> _statuses = 
    		new HashMap<String, Status>();
        public static final Map<String, Status> statuses = 
    		Collections.unmodifiableMap(_statuses);
    	private static final Map<String, String[]> _arrays = 
    		new HashMap<String, String[]>();
            
        static
        {
        	for (Status status : values())
        	{
        		_statuses.put(status.name(), status);
        		_arrays.put(status.name(), new String[]{status.name()});
        	}
        }
        
        private static final String [] UNSUBMITTED_STATUSES = {
        	Status.complete.name(), 
    		Status.submissionFailed.name()
    		};
        private static final String [] EMPTY_STRING_ARRAY = {};
        public static final String getUnsubmittedQuery()
        {
            return InstanceColumns.STATUS + " IN (?,?)";
        }
        public static String [] getUnsubmittedStatuses()
        {
        	return Arrays.asList(UNSUBMITTED_STATUSES).toArray(EMPTY_STRING_ARRAY);
        }
    }
    
    /**
     * Notes table
     */
    public static final class InstanceColumns implements BaseColumns {
        // This class cannot be instantiated
        private InstanceColumns() {}
        
        public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/instances");
        public static final String CONTENT_TYPE = "vnd.android.cursor.dir/vnd.odk.instance";
        public static final String CONTENT_ITEM_TYPE = "vnd.android.cursor.item/vnd.odk.instance";

        // These are the only things needed for an insert
        public static final String DISPLAY_NAME = "displayName";
        public static final String SUBMISSION_URI = "submissionUri";
        public static final String INSTANCE_FILE_PATH = "instanceFilePath";
        public static final String JR_FORM_ID = "jrFormId";
        //public static final String FORM_ID = "formId";
        
        // these are generated for you (but you can insert something else if you want)
        public static final String STATUS = "status";
        public static final String LAST_STATUS_CHANGE_DATE = "date";
        public static final String DISPLAY_SUBTEXT = "displaySubtext";
        //public static final String DISPLAY_SUB_SUBTEXT = "displaySubSubtext";


//        public static final String DEFAULT_SORT_ORDER = "modified DESC";
//        public static final String TITLE = "title";
//        public static final String NOTE = "note";
//        public static final String CREATED_DATE = "created";
//        public static final String MODIFIED_DATE = "modified";
    }
    
    /**
     * A meta-status that includes {@link Status#complete} and
     * {@link Status#submissionFailed}.  
     */
    public static final String UNSUBMITTED = "unsubmitted";

    /**
     * @return A {@link Cursor} to enumerate instances with the supplied
     * {@link Status}. You should {@link Activity#managedQuery(Uri, String[], String, String[], String) manage}
     * or {@link Cursor#close() close} this Cursor yourself.
     * @param statusString A {@link Status} converted to a String,
     * e.g. <code>Status.incomplete.name()</code>) or {@link #UNSUBMITTED}.
     * @param projection An array of columns to return, or <code>null</code>
     * for all columns.
     * @param order The SQL ORDER BY clause, or <code>null</code> for
     * no sorting.
     */
    public static Cursor getInstanceCursorByStatus(String statusString, 
    		String [] projection, String order)
    {
        String selection;
        String[] selectionArgs;

    	if (statusString == UNSUBMITTED)
    	{
    		selection = Status.getUnsubmittedQuery();
    		selectionArgs = Status.getUnsubmittedStatuses();
    	}
    	else
    	{
    		selection = InstanceColumns.STATUS + " = ?";
    		selectionArgs = Status._arrays.get(statusString);
    	}
        
        return Collect.getInstance().getContentResolver().query(
    		InstanceColumns.CONTENT_URI, projection, selection, selectionArgs, order);
    }
}
